public class num563 {
    //二叉树的坡度
    int ans;
    public int findTilt(TreeNode root) {
        dfs(root);//更新ans值，让其等于二叉树的坡度
        return ans;
    }

    private int dfs(TreeNode root) {
        if(root == null){
            return 0;
        }
        int left = dfs(root.left);
        int right = dfs(root.right);
        ans += Math.abs(left - right);
        return left + right + root.val;
    }
}
